<?php


namespace app\admin\controller\Web;

use app\admin\model\WebConfig;
use app\admincommon\controller\AdminController;
use think\facade\View;

class Config extends AdminController{

    protected $webconfig;


    public function __construct()
    {
        $this->webconfig=new WebConfig();

    }


    function index(){

        if(request()->isPost()){
            $post = request()->post();

            foreach ($post as $key => $value) {
                // 代码块
                try {
                    $this->webconfig->where(["config_name"=>$key])->update(["config_value"=>$value]);
                } catch (\Exception $e) {
                    $this->error('更新失败:try catch');
                }
            }

            $this->result(["code"=>1],1,"更新成功","json");

        }else{

            $data = $this->webconfig->select();
            $res=[];
            if(!empty($data)){
                for($i=0;$i<count($data);$i++){
                    $res[$data[$i]->config_name] = $data[$i]->config_value;
                }
            }
    
            View::assign(["webconfig" =>$res]);
            return View::fetch();
        }

    }

        






}